<?php


namespace EasySwoole\Rpc\Network;


use EasySwoole\Rpc\Protocol\Protocol;
use EasySwoole\Rpc\Protocol\Request;
use EasySwoole\Rpc\Protocol\Response;
use EasySwoole\Rpc\Server\ServiceNode;
use Swoole\Coroutine\Client;

class TcpClient
{
    private $client;

    function __construct(int $maxPackSize,float $timeout)
    {
        $this->client = new Client(SWOOLE_TCP);
        $this->client->set([
            'open_length_check' => true,
            'package_length_type'   => 'N',
            'package_length_offset' => 0,
            'package_body_offset'   => 4,
            'package_max_length'    => $maxPackSize,
            'timeout'=>$timeout
        ]);
    }

    function connect(ServiceNode $node)
    {
        return $this->client->connect($node->getIp(),$node->getPort());
    }

    function sendRequest(Request $request)
    {
        $data = $request->__tostring();
        $data = Protocol::pack($data);
        $len = strlen($data);
        if($this->client->send($data) !== $len){
            return false;
        }else{
            return true;
        }
    }

    function recv()
    {
        $res = new Response();
        $data = $this->client->recv();
        if($data){
            $data = Protocol::unpack($data);
            $json = json_decode($data,true);
            if(is_array($json)){
                $res->restore($json);
            }else{
                $res->setStatus(Response::STATUS_ILLEGAL_PACKAGE);
            }
        }else{
            $res->setStatus(Response::STATUS_SERVER_TIMEOUT);
        }
        return $res;
    }

    function __destruct()
    {
        if($this->client->isConnected()){
            $this->client->close();
        }
        unset($this->client);
    }
}